//
//  httpRequst.m
//  afNetWorkingDemo
//
//  Created by whalefin on 5/24/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

#import "httpRequst.h"

@implementation httpRequst

+(void)POST:(NSString *)URLString parameters:(id)parameters success:(void (^)(AFHTTPRequestOperation *, id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain",@"text/html", nil]];
    
//     manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation,error);
    }];
}


+(void)POST:(NSString *)URLString parameters:(id)parameters imagePath:(NSString *)filePath imageName:(NSString *)imageName keyWords:(NSString *)keyWords success:(void (^)(AFHTTPRequestOperation *, id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain",@"text/html", nil]];
    [manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
        if(filePath.length > 0){
            NSString *imageFileName = imageName;
            imageFileName = [imageFileName stringByAppendingString:@".jpg"];
            [formData appendPartWithFileURL:[NSURL fileURLWithPath:filePath] name:keyWords fileName:imageFileName mimeType:@"image/jpg" error:nil];
        }
    }success:^(AFHTTPRequestOperation *operation, id responseObject){
        success(operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation,error);
    }];

}

+(void)POST:(NSString *)URLString parameters:(id)parameters imagePathArray:(NSMutableArray *)filePathArray imageName:(NSString *)imageName keyWords:(NSString *)keyWords success:(void (^)(AFHTTPRequestOperation *, id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain",@"text/html", nil]];
    [manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
        if(filePathArray.count > 0){
            int i = 0;
            for (NSString *path in filePathArray) {
                NSString *imageFileName = imageName;
                imageFileName = [imageFileName stringByAppendingString:[NSString stringWithFormat:@"%d.jpg",i]];
//                [formDat]
                [formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:[NSString stringWithFormat:@"%@%d",keyWords,i] fileName:imageFileName mimeType:@"image/jpg" error:nil];
                i++ ;
            }
//            NSString *imageFileName = imageName;
//            imageFileName = [imageFileName stringByAppendingString:@".jpg"];
//            [formData appendPartWithFileURL:[NSURL fileURLWithPath:filePath] name:keyWords fileName:imageFileName mimeType:@"image/jpg" error:nil];
        }
    }success:^(AFHTTPRequestOperation *operation, id responseObject){
        success(operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation,error);
    }];
    
}


+(void)GET:(NSString *)URLString parameters:(id)parameters success:(void (^)(AFHTTPRequestOperation *, id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain",@"text/html", nil]];
    [manager GET:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation,error);
    }];
}


@end
